package com.stx.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.stx.dao.CommentDao;
import com.stx.entity.Film_comment;
import com.stx.entity.User_collection;

public class CommentDaoImpl implements CommentDao{

	private HibernateTemplate sessionFactory;

	public HibernateTemplate getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(HibernateTemplate sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void addCommentDao(Film_comment film_comment) {
		sessionFactory.save(film_comment);
		sessionFactory.flush();
		
	}

	  public<T> List<T> excuteSQLQuery(String sql, Object[] obj, Class<T> entityType){
	        final String tempSql = sql;
	        final Object[] tempObj = obj;
	        final Class<T> tempEntity = entityType;
	        
	        return sessionFactory.execute(new HibernateCallback() {
	            public Object doInHibernate(Session session) throws HibernateException, SQLException {
	                SQLQuery q = session.createSQLQuery(tempSql);// 原文: SQLQuery q = getSession().createSQLQuery(tempSql);
	                if(tempObj != null && tempObj.length > 0){
	                    for(int i = 0;i < tempObj.length; i++){
	                        q.setParameter(i, tempObj[i]);
	                    }
	                }
	                return q.addEntity(tempEntity).list();
	            }
	        });
	        
	    }

	@Override
	public List getNewFilm_comment() {
		//select *from(select * from fc_film where fi_state=? order by dbms_random.value)where rownum <= ?";
		String sql="select * from(select * from FC_SMALL_COMMENT  order by SC_TIME desc)where rownum <= ?";
		Object[] obj={1};
		return excuteSQLQuery(sql, obj, Film_comment.class);
	}

}
